In [1]:
import numpy as np
import tensorflow as tf
from tensorflow import keras
from sklearn import metrics
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dense, Flatten, BatchNormalization, Conv2D, MaxPool2D
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import confusion_matrix
import itertools
import os
import shutil
from random import randint
import random
import glob
import matplotlib.pyplot as plt
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
%matplotlib inline
In C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\mpl-data\stylelib\_classic_test.mplstyle: 
The text.latex.preview rcparam was deprecated in Matplotlib 3.3 and will be removed two minor releases later.
In C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\mpl-data\stylelib\_classic_test.mplstyle: 
The mathtext.fallback_to_cm rcparam was deprecated in Matplotlib 3.3 and will be removed two minor releases later.
In C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\mpl-data\stylelib\_classic_test.mplstyle: Support for setting the 'mathtext.fallback_to_cm' rcParam is deprecated since 3.3 and will be removed two minor releases later; use 'mathtext.fallback : 'cm' instead.
In C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\mpl-data\stylelib\_classic_test.mplstyle: 
The validate_bool_maybe_none function was deprecated in Matplotlib 3.3 and will be removed two minor releases later.
In C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\mpl-data\stylelib\_classic_test.mplstyle: 
The savefig.jpeg_quality rcparam was deprecated in Matplotlib 3.3 and will be removed two minor releases later.
In C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\mpl-data\stylelib\_classic_test.mplstyle: 
The keymap.all_axes rcparam was deprecated in Matplotlib 3.3 and will be removed two minor releases later.
In C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\mpl-data\stylelib\_classic_test.mplstyle: 
The animation.avconv_path rcparam was deprecated in Matplotlib 3.3 and will be removed two minor releases later.
In C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\mpl-data\stylelib\_classic_test.mplstyle: 
The animation.avconv_args rcparam was deprecated in Matplotlib 3.3 and will be removed two minor releases later.

1. Image Preparation for Convolutional Neural Network with TensorFlow's Keras API

The images included in the data/skin-cancer directory are a random subset of microscopic images of Benign & Malignant Skin Cancer cells from ISIC Data-Base: https://www.isic-archive.com/#!/topWithHeader/onlyHeaderTop/gallery

In [2]:
# Organize data into train directories
os.chdir('C:\\Users\\רועי\\data\\skin-cancer\\benign_set')
if os.path.isdir('C:\\Users\\רועי\\data\\skin-cancer\\train\\benign') is False:
    os.makedirs('C:\\Users\רועי\\data\\skin-cancer\\train\\benign')
    
    for i in random.sample(glob.glob('*'), 500):
        shutil.move(i, 'C:\\Users\\רועי\\data\\skin-cancer\\train\\benign')  
os.chdir('../../')
In [3]:
# Organize data into train directories
os.chdir('C:\\Users\\רועי\\data\\skin-cancer\\malignant_set')
if os.path.isdir('C:\\Users\\רועי\\data\\skin-cancer\\train\\malignant') is False:
    os.makedirs('C:\\Users\רועי\\data\\skin-cancer\\train\\malignant')
    
    for i in random.sample(glob.glob('*'), 500):
        shutil.move(i, 'C:\\Users\\רועי\\data\\skin-cancer\\train\\malignant')  
os.chdir('../../')
In [4]:
# Organize data into valid directories
os.chdir('C:\\Users\\רועי\\data\\skin-cancer\\benign_set')
if os.path.isdir('C:\\Users\\רועי\\data\\skin-cancer\\valid\\benign') is False:
    os.makedirs('C:\\Users\רועי\\data\\skin-cancer\\valid\\benign')
    
    for i in random.sample(glob.glob('*'), 100):
        shutil.move(i, 'C:\\Users\\רועי\\data\\skin-cancer\\valid\\benign')  
os.chdir('../../')
In [5]:
# Organize data into valid directories
os.chdir('C:\\Users\\רועי\\data\\skin-cancer\\malignant_set')
if os.path.isdir('C:\\Users\\רועי\\data\\skin-cancer\\valid\\malignant') is False:
    os.makedirs('C:\\Users\רועי\\data\\skin-cancer\\valid\\malignant')
    
    for i in random.sample(glob.glob('*'), 100):
        shutil.move(i, 'C:\\Users\\רועי\\data\\skin-cancer\\valid\\malignant')  
os.chdir('../../')
In [6]:
train_path = 'C:\\Users\\רועי\\\data\\skin-cancer\\train'
valid_path = 'C:\\Users\\רועי\\\data\\skin-cancer\\valid'
In [7]:
train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input).flow_from_directory(directory=train_path, target_size=(224,224), classes=['benign','malignant'], batch_size=10)
valid_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input).flow_from_directory(directory=valid_path, target_size=(224,224), classes=['benign','malignant'], batch_size=10)
Found 1000 images belonging to 2 classes.
Found 200 images belonging to 2 classes.
In [8]:
assert train_batches.n == 1000
assert valid_batches.n == 200
assert train_batches.num_classes == valid_batches.num_classes == 2
In [9]:
# plot images in the form of a 1 by 10 grid and resize img to 20x20
def plotImages(images_arr):
    fig, axes = plt.subplots(1, 10, figsize=(20,20))
    axes = axes.flatten()
    for img, ax in zip( images_arr, axes):
        ax.imshow(img)
        ax.axis('off')
    plt.tight_layout()
    plt.show()
In [10]:
train_imgs, train_labels = next(train_batches)
plotImages(train_imgs)
print(train_labels)
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
[[0. 1.]
 [1. 0.]
 [1. 0.]
 [1. 0.]
 [1. 0.]
 [0. 1.]
 [0. 1.]
 [1. 0.]
 [0. 1.]
 [0. 1.]]

2. Create a Convolutional Neural Network with TensorFlow's Keras API

In [11]:
pysical_devices = tf.config.experimental.list_physical_devices('GPU')
print("Num GPUs Available: ", len(pysical_devices))
Num GPUs Available:  0
In [12]:
model = Sequential([
    Conv2D(filters=32, kernel_size=(3, 3), activation='relu', padding = 'same', input_shape=(224,224,3)),
    MaxPool2D(pool_size=(2, 2), strides=2),
    Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding = 'same'),
    MaxPool2D(pool_size=(2, 2), strides=2),
    Flatten(),
    Dense(units=2, activation='softmax')
])
WARNING:tensorflow:From C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
In [13]:
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 224, 224, 32)      896       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 112, 112, 32)      0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 112, 112, 64)      18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 56, 56, 64)        0         
_________________________________________________________________
flatten (Flatten)            (None, 200704)            0         
_________________________________________________________________
dense (Dense)                (None, 2)                 401410    
=================================================================
Total params: 420,802
Trainable params: 420,802
Non-trainable params: 0
_________________________________________________________________
In [14]:
model.compile(optimizer=Adam(learning_rate=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])

3. Train a Convolutional Neural Network with TensorFlow's Keras API

In [15]:
X_train = train_batches
y_train = train_batches.classes
In [16]:
model.fit(x=X_train, validation_data=valid_batches, epochs=10, verbose=2)
Epoch 1/10
100/100 - 579s - loss: 13.1146 - acc: 0.6710 - val_loss: 2.3796 - val_acc: 0.8000
Epoch 2/10
100/100 - 498s - loss: 2.3837 - acc: 0.8200 - val_loss: 1.5819 - val_acc: 0.8050
Epoch 3/10
100/100 - 505s - loss: 1.1106 - acc: 0.8580 - val_loss: 1.9547 - val_acc: 0.7700
Epoch 4/10
100/100 - 488s - loss: 1.4272 - acc: 0.8570 - val_loss: 1.4220 - val_acc: 0.8150
Epoch 5/10
100/100 - 495s - loss: 0.2600 - acc: 0.9520 - val_loss: 1.2719 - val_acc: 0.8250
Epoch 6/10
100/100 - 497s - loss: 0.1111 - acc: 0.9640 - val_loss: 1.1389 - val_acc: 0.8550
Epoch 7/10
100/100 - 490s - loss: 0.0317 - acc: 0.9870 - val_loss: 1.0048 - val_acc: 0.8550
Epoch 8/10
100/100 - 618s - loss: 0.0117 - acc: 0.9960 - val_loss: 0.9839 - val_acc: 0.8300
Epoch 9/10
100/100 - 605s - loss: 0.0053 - acc: 0.9990 - val_loss: 1.0217 - val_acc: 0.8300
Epoch 10/10
100/100 - 273s - loss: 0.0015 - acc: 1.0000 - val_loss: 1.0338 - val_acc: 0.8400
Out[16]:
<tensorflow.python.keras.callbacks.History at 0x18e25878148>

4. Build a Test Set with TensorFlow's Keras API

In [17]:
# Organize data into test directories
os.chdir('C:\\Users\רועי\\data\\skin-cancer\\benign_set')
if os.path.isdir('C:\\Users\\רועי\\data\\skin-cancer\\test\\benign') is False:
    os.makedirs('C:\\Users\רועי\\data\\skin-cancer\\test\\benign')
    
    for i in random.sample(glob.glob('*'), 50):
        shutil.move(i, 'C:\\Users\\רועי\\data\\skin-cancer\\test\\benign')  
os.chdir('../../')
In [18]:
# Organize data into test directories
os.chdir('C:\\Users\רועי\\data\\skin-cancer\\malignant_set')
if os.path.isdir('C:\\Users\\רועי\\data\\skin-cancer\\test\\malignant') is False:
    os.makedirs('C:\\Users\רועי\\data\\skin-cancer\\test\\malignant')
    
    for i in random.sample(glob.glob('*'), 50):
        shutil.move(i, 'C:\\Users\\רועי\\data\\skin-cancer\\test\\malignant')  
os.chdir('../../')
In [19]:
test_path  = 'C:\\Users\\רועי\\\data\\skin-cancer\\test'
In [20]:
test_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input).flow_from_directory(directory=test_path, target_size=(224,224),   classes=['benign','malignant'], batch_size=10, shuffle=False)
Found 100 images belonging to 2 classes.
In [21]:
assert test_batches.n == 100
assert test_batches.num_classes == 2

5. Neural Network Predictions with TensorFlow's Keras API

In [22]:
test_imgs, test_labels = next(test_batches)
plotImages(test_imgs)
print(test_labels)
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
[[1. 0.]
 [1. 0.]
 [1. 0.]
 [1. 0.]
 [1. 0.]
 [1. 0.]
 [1. 0.]
 [1. 0.]
 [1. 0.]
 [1. 0.]]
In [23]:
X_test = test_batches
y_test = test_batches.classes
In [24]:
predictions = model.predict(x=X_test, verbose=0)
In [25]:
for i in predictions:
    print(i)
[0.9802997 0.0197003]
[1.9541885e-08 1.0000000e+00]
[9.994773e-01 5.227418e-04]
[0.00325469 0.99674535]
[0.9803562  0.01964379]
[1.000000e+00 8.721857e-12]
[9.9999475e-01 5.2028295e-06]
[0.99301404 0.00698605]
[0.66946095 0.3305391 ]
[9.9999416e-01 5.8842506e-06]
[0.03981518 0.9601849 ]
[9.999914e-01 8.576671e-06]
[0.99464583 0.00535418]
[0.9292744  0.07072559]
[1.0000000e+00 2.8359781e-19]
[1.0000000e+00 1.7173389e-10]
[1.000000e+00 9.781411e-14]
[1.0000000e+00 1.9528143e-11]
[1.000000e+00 7.286939e-12]
[9.9978834e-01 2.1172139e-04]
[1.0000000e+00 2.0280114e-27]
[9.950802e-04 9.990049e-01]
[1. 0.]
[1.000000e+00 2.120807e-15]
[1. 0.]
[1. 0.]
[1.0000000e+00 5.3565987e-08]
[0.00403944 0.99596053]
[1.0000000e+00 6.4680587e-20]
[1. 0.]
[1.000000e+00 9.792155e-12]
[1.0000000e+00 2.0098035e-21]
[1.0000000e+00 2.2461985e-21]
[1.0000000e+00 4.5110513e-16]
[1. 0.]
[1.0000000e+00 2.2244064e-11]
[1.0000000e+00 5.6124617e-14]
[1.000000e+00 2.473145e-08]
[1.0000000e+00 1.8341895e-13]
[1.0000000e+00 8.5572565e-12]
[1.0000000e+00 1.3444635e-20]
[1.0000000e+00 5.4065346e-12]
[1.0000000e+00 1.2959128e-17]
[1.00000e+00 3.62774e-20]
[1.0000000e+00 1.4757555e-33]
[0.42813927 0.57186073]
[0.00120855 0.9987915 ]
[1.0000000e+00 3.7286467e-12]
[0.99190384 0.00809613]
[9.9999893e-01 1.1314592e-06]
[1.0000000e+00 5.7745243e-08]
[0.00854086 0.99145913]
[0.22417067 0.7758293 ]
[0.43888834 0.5611116 ]
[0.26669112 0.7333089 ]
[0.45606795 0.543932  ]
[0.21383072 0.7861693 ]
[0.00431142 0.9956886 ]
[0.95266795 0.04733207]
[0.27643257 0.7235674 ]
[0.00481859 0.9951814 ]
[0.03337603 0.9666239 ]
[0.00790401 0.99209595]
[1.7497321e-09 1.0000000e+00]
[0.8653706  0.13462947]
[0.00562621 0.9943738 ]
[0.07484119 0.9251588 ]
[0.87349486 0.1265051 ]
[9.6533836e-07 9.9999905e-01]
[0.00924238 0.99075764]
[2.2736973e-15 1.0000000e+00]
[0.01097289 0.989027  ]
[7.0818296e-06 9.9999297e-01]
[3.8524334e-08 1.0000000e+00]
[4.7053945e-07 9.9999952e-01]
[3.7919995e-04 9.9962080e-01]
[1.1520672e-05 9.9998844e-01]
[2.8674689e-04 9.9971324e-01]
[1.2503936e-13 1.0000000e+00]
[0.8784751  0.12152491]
[6.130846e-09 1.000000e+00]
[3.0794214e-05 9.9996924e-01]
[0.5079948  0.49200517]
[0.05475643 0.9452436 ]
[3.5686924e-12 1.0000000e+00]
[6.224692e-06 9.999938e-01]
[3.3181047e-16 1.0000000e+00]
[7.063111e-11 1.000000e+00]
[6.564781e-07 9.999993e-01]
[1.7872693e-14 1.0000000e+00]
[7.005421e-18 1.000000e+00]
[2.4728772e-06 9.9999750e-01]
[0.3644426 0.6355574]
[9.059617e-04 9.990940e-01]
[1.1731001e-05 9.9998832e-01]
[2.7068011e-06 9.9999726e-01]
[2.1652606e-05 9.9997830e-01]
[0.02946153 0.9705385 ]
[2.1866227e-07 9.9999976e-01]
[3.3097586e-10 1.0000000e+00]
In [26]:
rounded_predictions = np.argmax(np.round(predictions), axis=-1)  
In [27]:
for i in rounded_predictions:
    print(i)
0
1
0
1
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
0
0
0
0
1
1
1
1
1
1
1
0
1
1
1
1
1
0
1
1
0
1
1
1
1
1
1
1
1
1
1
1
0
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

6. Create a Confusion Matrix for Neural Network Predictions

In [28]:
y_pred=rounded_predictions
In [29]:
cm = confusion_matrix(y_true=y_test, y_pred=y_pred)
In [30]:
def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i, j],
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
In [31]:
cm_plot_labels = ['benign','malignant']
plot_confusion_matrix(cm=cm, classes=cm_plot_labels, title='Confusion Matrix')
Confusion matrix, without normalization
[[43  7]
 [ 6 44]]
In [32]:
print("\033[1m The result is telling us that we have: ",(cm[0,0]+cm[1,1]),"correct predictions.")
print("\033[1m The result is telling us that we have: ",(cm[0,1]+cm[1,0]),"incorrect predictions.")
print("\033[1m We have a total predictions of: ",(cm.sum()))
 The result is telling us that we have:  87 correct predictions.
 The result is telling us that we have:  13 incorrect predictions.
 We have a total predictions of:  100

7. Create a Classification Report for Neural Network Predictions

In [33]:
from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred))
              precision    recall  f1-score   support

           0       0.88      0.86      0.87        50
           1       0.86      0.88      0.87        50

    accuracy                           0.87       100
   macro avg       0.87      0.87      0.87       100
weighted avg       0.87      0.87      0.87       100

In [34]:
#calculate Accuracy, how often is the classifier correct?
print("Accuracy of the Convolutional Neural Network model:", "{:.2%}".format(metrics.accuracy_score(y_test, y_pred)))
print("\nWell, you got a classification rate of", "{:.2%}".format(metrics.accuracy_score(y_test, y_pred)))
Accuracy of the Convolutional Neural Network model: 87.00%

Well, you got a classification rate of 87.00%
In [35]:
#calculate Precision
print("Precision of the Convolutional Neural Network model:", "{:.2%}".format(metrics.precision_score(y_test, y_pred)))
print("\nPrecision: Precision is about being precise, i.e., how precise our model is. In other words, we can say, when a model makes a prediction, how often it is correct. In our prediction case, when our Convolutional Neural Network model predicted an image is of a malignant skin cancer cell, that image is actually of a malignant skin cancer cell", "{:.2%}".format(metrics.precision_score(y_test, y_pred)) ,"of the time.")
Precision of the Convolutional Neural Network model: 86.27%

Precision: Precision is about being precise, i.e., how precise our model is. In other words, we can say, when a model makes a prediction, how often it is correct. In our prediction case, when our Convolutional Neural Network model predicted an image is of a malignant skin cancer cell, that image is actually of a malignant skin cancer cell 86.27% of the time.
In [36]:
#calculate Recall
print("Recall of the Convolutional Neural Network model:", "{:.2%}".format(metrics.recall_score(y_test, y_pred)))
print("\nRecall: If there is an image of a malignant skin cancer cell present in the test set, our Convolutional Neural Network model can identify it", "{:.2%}".format(metrics.recall_score(y_test, y_pred)) ,"of the time.")
Recall of the Convolutional Neural Network model: 88.00%

Recall: If there is an image of a malignant skin cancer cell present in the test set, our Convolutional Neural Network model can identify it 88.00% of the time.
In [37]:
from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve
cnn_roc_auc = roc_auc_score(y_test, model.predict(X_test)[:,1])
fpr, tpr, thresholds = roc_curve(y_test, model.predict_proba(X_test)[:,1])
plt.figure()
plt.plot(fpr, tpr, label='Convolutional Neural Network (area = %0.4f)' % cnn_roc_auc)
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic')
plt.legend(loc="lower right")
plt.savefig('CNN_ROC')
plt.show()

8. Deployment of the Convolutional Neural Network

In [38]:
randy = randint(0,1)
if randy == 0:
    os.chdir('C:\\Users\\רועי\\data\\skin-cancer\\benign_set')
    if os.path.isdir('C:\\Users\\רועי\\data\\skin-cancer\\pred\\benign') is False:
        os.makedirs('C:\\Users\רועי\\data\\skin-cancer\\pred\\benign')
        for i in random.sample(glob.glob('*'), 1):
            shutil.move(i, 'C:\\Users\\רועי\\data\\skin-cancer\\pred\\benign')  
    os.chdir('../../')
else:
    os.chdir('C:\\Users\\רועי\\data\\skin-cancer\\malignant_set')
    if os.path.isdir('C:\\Users\\רועי\\data\\skin-cancer\\pred\\malignant') is False:
        os.makedirs('C:\\Users\רועי\\data\\skin-cancer\\pred\\malignant')
        for i in random.sample(glob.glob('*'), 1):
            shutil.move(i, 'C:\\Users\\רועי\\data\\skin-cancer\\pred\\malignant')  
    os.chdir('../../')

pred_path = 'C:\\Users\רועי\\data\\skin-cancer\\pred'

if randy == 0:
    pred_pic1 = 'C:\\Users\רועי\\data\\skin-cancer\\pred\\benign' 
else:
    pred_pic1 = 'C:\\Users\רועי\\data\\skin-cancer\\pred\\malignant'
    
pred_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input).flow_from_directory(directory=pred_path, target_size=(224,224),   classes=['benign','malignant'], batch_size=10, shuffle=False)

assert pred_batches.n == 1
assert pred_batches.num_classes == 2

from IPython.display import Image
if randy == 0:
    os.chdir('C:\\Users\רועי\\data\\skin-cancer\\pred\\benign')
    for i in random.sample(glob.glob('*'), 1):
        Image(filename=i)

else:
    os.chdir('C:\\Users\רועי\\data\\skin-cancer\\pred\\malignant')
    for i in random.sample(glob.glob('*'), 1):
        Image(filename=i)
    
Image(filename=i)
Found 1 images belonging to 2 classes.
Out[38]:
In [39]:
if randy == 0:
    print("\033[1m This is an image of a benign skin cancer cell")
else:
    print("\033[1m This is an image of a malignant skin cancer cell")
 This is an image of a malignant skin cancer cell
In [40]:
accuracy = "{:.2%}".format(metrics.accuracy_score(y_test, y_pred))
pred_imgs, pred_labels = next(pred_batches)
X_pred = pred_batches
y_pred = pred_batches.classes
pred = model.predict(x=X_pred, verbose=0)
rounded_pred = np.argmax(np.round(pred), axis=-1) 
if rounded_pred[0] == 0:
    print("\033[1m The algorithm predict, with accuracy (i.e., confidence level) of", accuracy, "that this is an image of a benign skin cancer cell")
else:
    print("\033[1m The algorithm predict, with accuracy (i.e., confidence level) of", accuracy, "that this is an image of a malignant skin cancer cell")
 The algorithm predict, with accuracy (i.e., confidence level) of 87.00% that this is an image of a malignant skin cancer cell

9. Save and Load a Model with TensorFlow's Keras API

1. model.save()

In [41]:
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 224, 224, 32)      896       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 112, 112, 32)      0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 112, 112, 64)      18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 56, 56, 64)        0         
_________________________________________________________________
flatten (Flatten)            (None, 200704)            0         
_________________________________________________________________
dense (Dense)                (None, 2)                 401410    
=================================================================
Total params: 420,802
Trainable params: 420,802
Non-trainable params: 0
_________________________________________________________________
In [42]:
# Checks first to see if file exists already
# If not, the model is saved to disk.
import os.path
if os.path.isfile('C:\\Users\\רועי\\models\\skincancer_model.h7') is False:
    model.save('C:\\Users\\רועי\\models\skincancer_model.h7')

This save functions saves:

  1. The architecture of the model allowing to re-create the moddel.
  2. The weights of the model.
  3. The training configuration (loss, optimizer).
  4. The state of the optimizer, allowing to resume training exactly where you left off.
In [43]:
from tensorflow.keras.models import load_model
new_model = load_model('C:\\Users\\רועי\\models\\skincancer_model.h7')
WARNING:tensorflow:From C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\init_ops.py:97: calling GlorotUniform.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
WARNING:tensorflow:From C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\init_ops.py:97: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
In [44]:
new_model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 224, 224, 32)      896       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 112, 112, 32)      0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 112, 112, 64)      18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 56, 56, 64)        0         
_________________________________________________________________
flatten (Flatten)            (None, 200704)            0         
_________________________________________________________________
dense (Dense)                (None, 2)                 401410    
=================================================================
Total params: 420,802
Trainable params: 420,802
Non-trainable params: 0
_________________________________________________________________
In [45]:
new_model.get_weights()
Out[45]:
[array([[[[ 1.38012096e-01, -8.84406865e-02,  1.20834507e-01,
            1.20329633e-01, -1.16766378e-01,  7.45493323e-02,
           -1.34152964e-01, -1.15893736e-01, -8.40075389e-02,
           -7.46510699e-02,  1.33424535e-01, -7.51701277e-03,
           -3.67834829e-02,  8.52866620e-02, -1.00812070e-01,
           -1.64543353e-02, -9.01955739e-03,  1.06778115e-01,
           -7.34757707e-02,  7.54573047e-02, -5.29317670e-02,
            9.08764452e-02, -1.27979338e-01, -3.71927186e-03,
           -2.38801409e-02, -1.02364317e-01, -1.08862691e-01,
            1.25361934e-01, -8.83691981e-02,  3.83781604e-02,
            7.98096731e-02, -1.16856664e-01],
          [ 6.22796603e-02, -7.23754466e-02, -9.91126373e-02,
            8.96538347e-02,  2.31015757e-02, -7.83761218e-02,
            2.62644459e-02,  1.14494003e-01,  9.14754421e-02,
            1.14326626e-02, -7.66148865e-02, -7.30372369e-02,
           -2.92360950e-02, -1.10828280e-01,  1.34554029e-01,
            4.18831706e-02,  7.84734264e-02,  6.00380450e-02,
            3.76507267e-02,  6.89542666e-02, -3.90463658e-02,
           -2.22085230e-02,  7.78661668e-02, -2.86889281e-02,
            5.52291125e-02,  1.38606355e-01, -1.17721699e-01,
            8.06322247e-02, -1.08877674e-01,  5.86248301e-02,
            3.91125344e-02, -6.37635123e-03],
          [ 3.61001119e-02, -1.27298340e-01,  1.30918235e-01,
           -1.31724015e-01,  9.87402648e-02,  3.85304652e-02,
           -7.65362382e-02, -6.56289235e-02, -1.11280508e-01,
            4.79274429e-02,  6.01455308e-02,  4.20850702e-02,
           -1.11730240e-01,  1.21583000e-01,  1.21500619e-01,
           -1.10844269e-01,  4.99703325e-02, -5.36879003e-02,
            8.25554430e-02,  1.33216411e-01,  4.82029915e-02,
           -3.65616940e-03,  2.61972640e-02, -5.84965274e-02,
           -5.09034283e-02, -6.61584884e-02, -8.41583759e-02,
           -1.76085159e-02,  9.63048041e-02,  2.90775560e-02,
           -9.25419182e-02,  2.50847749e-02]],
 
         [[-3.15915272e-02,  2.53626592e-02,  1.33855894e-01,
            2.09373999e-02,  6.36935234e-03,  3.22284326e-02,
            3.48305106e-02, -4.83667888e-02, -7.72036910e-02,
            8.96398202e-02, -7.23013952e-02,  5.20349555e-02,
            8.89291149e-03, -5.59077263e-02,  1.18802696e-01,
           -8.89856219e-02, -1.26309574e-01, -1.34811714e-01,
           -3.31439786e-02,  8.70612338e-02, -8.85880888e-02,
           -1.00435272e-01, -1.22203670e-01,  3.76036303e-04,
            1.24958288e-02, -8.77540112e-02, -1.06317505e-01,
            7.50828534e-03,  5.79860210e-02, -8.18044990e-02,
            9.27680079e-03,  3.20650339e-02],
          [ 5.66818984e-03,  9.60438251e-02,  1.03375025e-01,
           -6.91270158e-02, -1.13606259e-01, -1.03402346e-01,
            6.85153231e-02,  8.88056457e-02, -2.60776980e-03,
            2.01513823e-02, -5.19283079e-02,  1.24803662e-01,
            3.14342082e-02,  6.79740608e-02, -5.35403453e-02,
            8.64978433e-02,  3.15252990e-02, -1.17797509e-01,
            1.26365453e-01,  1.27379149e-01, -1.14447400e-01,
            7.25468621e-02, -6.65635914e-02,  7.70748034e-02,
            9.81387496e-02, -1.19082496e-01,  3.80955823e-02,
           -1.71924811e-02,  1.09196484e-01, -3.52888443e-02,
            1.22958906e-01, -5.22010997e-02],
          [ 2.16924120e-02,  4.22612652e-02,  1.20890569e-02,
            9.90278721e-02, -3.31160054e-02, -8.78909826e-02,
           -6.75626621e-02, -1.26639307e-01,  3.04759704e-02,
            1.12492248e-01,  2.66952836e-03,  2.58892328e-02,
            5.63528538e-02, -7.36360550e-02,  8.67929831e-02,
            6.73651546e-02,  4.20602523e-02,  1.28744513e-01,
           -9.33288112e-02,  4.83517163e-02, -4.72525917e-02,
            5.80399148e-02, -1.21989466e-01,  2.34387387e-02,
            8.17174837e-02, -1.22700319e-01, -3.91919874e-02,
           -6.88635111e-02, -4.14893106e-02,  3.90252955e-02,
            8.22026059e-02,  3.04137310e-03]],
 
         [[-1.13243967e-01, -3.03858742e-02, -6.74555302e-02,
           -1.39478836e-02,  9.55590904e-02,  4.94423844e-02,
            1.09593824e-01, -1.04166038e-01,  1.19421184e-01,
            1.33578241e-01,  1.34681180e-01, -1.29124016e-01,
            7.17445239e-02, -6.99853152e-02, -9.13952813e-02,
           -1.03688776e-01, -8.90705958e-02, -5.87549387e-03,
            1.12756640e-02,  8.68401229e-02,  1.16083793e-01,
           -6.10074438e-02, -6.23488938e-03, -9.02798325e-02,
           -1.39221489e-01,  4.75379862e-02,  9.52165946e-02,
            8.69382522e-04,  3.52889001e-02,  1.76504068e-02,
           -5.01421420e-03,  1.14564329e-01],
          [-1.18791334e-01,  3.26167345e-02,  9.21265706e-02,
           -5.66888489e-02, -1.18214086e-01,  1.17234543e-01,
           -6.53363690e-02,  1.35980666e-01,  6.83877841e-02,
           -3.59751135e-02, -2.26282962e-02,  3.10736746e-02,
            5.35915280e-03,  1.15953676e-01,  2.65577622e-02,
           -5.25063463e-02,  5.42227440e-02, -1.06632203e-01,
            2.31003687e-02, -1.29438881e-02,  1.05057798e-01,
            7.69168884e-02,  2.62694396e-02,  1.15889952e-01,
            6.47801161e-02, -1.29644290e-01,  8.36851299e-02,
            1.10428073e-02, -1.13662958e-01, -1.07159674e-01,
           -1.14953578e-01,  1.01206966e-01],
          [-4.43135127e-02,  7.92076513e-02, -5.78693040e-02,
           -7.15398788e-02,  4.26418781e-02,  7.95254111e-02,
           -1.54244145e-02,  7.77518824e-02,  1.19091749e-01,
           -1.09476142e-01, -4.59219851e-02, -9.14272442e-02,
           -1.38114691e-01, -5.66439070e-02,  1.20284326e-01,
           -1.31065682e-01, -1.06112145e-01,  1.02248743e-01,
           -4.86445948e-02, -1.31601304e-01, -2.96466034e-02,
           -1.15997784e-01,  7.95469135e-02, -1.05960675e-01,
            1.06837176e-01,  2.44174805e-02, -7.40103349e-02,
            7.50668496e-02, -6.00052737e-02,  3.80902295e-03,
            1.27229929e-01, -3.35594006e-02]]],
 
 
        [[[ 2.45169364e-03, -2.86237709e-03, -7.98791274e-03,
           -3.73667739e-02,  8.03174004e-02, -1.96691155e-02,
           -7.77103677e-02,  8.98281559e-02,  6.34301677e-02,
           -1.31189048e-01,  1.03952296e-01, -1.11763380e-01,
           -4.62341793e-02, -2.72931773e-02,  1.03552558e-01,
           -7.65201598e-02, -9.07593891e-02,  7.19395727e-02,
            1.85596962e-02,  1.29504250e-02,  9.46459398e-02,
            5.51584223e-03,  1.27091214e-01, -1.29904985e-01,
            5.59513867e-02,  5.19930013e-03, -1.87762864e-02,
           -7.96043873e-02, -1.23457998e-01,  7.20390752e-02,
            1.13124810e-01,  5.71398577e-03],
          [ 6.25795797e-02, -1.06990732e-01, -9.79946852e-02,
            6.59460016e-03, -8.50046203e-02, -8.58902559e-02,
            8.57231915e-02,  1.17119998e-01, -5.60670830e-02,
           -1.94190610e-02, -3.34095918e-02, -2.71843970e-02,
            2.18488853e-02, -9.40648392e-02, -1.90243274e-02,
            4.97210249e-02, -1.99519899e-02,  7.12958677e-03,
            4.99475449e-02,  1.19867943e-01, -8.87091309e-02,
           -1.38903588e-01,  5.06118033e-03, -5.03636375e-02,
           -6.19172826e-02, -1.91012323e-02,  1.34429395e-01,
           -1.01476595e-01, -4.62766513e-02, -4.67154644e-02,
           -6.39601785e-04,  1.18888162e-01],
          [-6.51676804e-02, -7.03894868e-02,  1.29924342e-01,
           -6.35272264e-02, -7.22595155e-02,  1.01845413e-01,
            1.24218494e-01, -7.11901784e-02, -5.47299124e-02,
            5.08526266e-02, -5.13649695e-02,  1.03991918e-01,
            8.51125494e-02, -7.28062689e-02, -1.24846175e-01,
            8.29203501e-02, -5.74660972e-02,  5.76177835e-02,
            1.16232425e-01,  8.45944136e-02,  1.04224242e-01,
            5.60795702e-02,  5.69354407e-02, -3.90475169e-02,
           -1.19932309e-01, -4.73300591e-02,  3.23001780e-02,
           -6.47194833e-02,  4.57348712e-02, -9.27224085e-02,
            6.17517084e-02,  9.22940075e-02]],
 
         [[ 2.97921933e-02, -8.22824463e-02, -4.17748047e-03,
            1.81095563e-02, -4.31207381e-02, -5.98638989e-02,
            2.99708098e-02, -1.87734980e-02, -9.46068019e-02,
            1.25313342e-01,  9.58209112e-02,  5.96114136e-02,
            5.12314178e-02, -1.05512291e-01,  9.73367542e-02,
           -9.00197849e-02, -2.69910377e-02,  5.38100861e-02,
            7.14058951e-02,  1.75184663e-02, -1.70577932e-02,
            1.77334137e-02,  9.67472047e-02,  1.08446158e-01,
           -1.32045284e-01,  9.20715779e-02, -1.28105298e-01,
            1.03573970e-01,  1.10896885e-01, -3.42792794e-02,
           -4.08363976e-02,  6.88628703e-02],
          [-1.87277514e-02, -1.51816308e-02, -1.36438102e-01,
           -4.25186427e-03, -9.71929803e-02, -1.01062413e-02,
           -7.59762526e-02,  1.15712196e-01, -4.07594116e-03,
            4.57760356e-02,  9.58429873e-02, -1.00841098e-01,
           -4.76827435e-02, -7.66278729e-02, -6.37250319e-02,
           -6.07004017e-02,  1.26912490e-01,  9.40351561e-02,
           -1.13452397e-01,  8.79292637e-02, -2.41072904e-02,
            8.87157843e-02, -3.79834585e-02, -3.73128578e-02,
           -1.16166994e-01,  6.98104650e-02, -4.75598779e-03,
            6.04438558e-02, -1.26588494e-01,  1.28325060e-01,
            4.31407653e-02, -1.32414192e-01],
          [-1.05637647e-01,  4.17511724e-02, -2.84398105e-02,
           -6.70498759e-02,  5.82432793e-03, -3.19725648e-02,
            1.20018758e-01,  7.82972202e-02, -8.03264230e-02,
            1.26096904e-01,  2.75085196e-02,  2.59983093e-02,
            8.83707777e-02,  9.00678933e-02,  1.12566382e-01,
            5.96420020e-02,  8.65440741e-02,  7.00573111e-03,
           -1.10285155e-01, -6.39790148e-02,  1.25691622e-01,
            1.24573223e-01,  1.07342936e-01, -2.40996126e-02,
            1.30076900e-01,  3.07643805e-02, -1.09452590e-01,
            5.82771143e-03,  1.21228993e-01, -1.24223426e-01,
            5.31139597e-02, -2.95768064e-02]],
 
         [[-1.14101872e-01,  1.30948722e-01,  3.64383347e-02,
           -9.63269621e-02,  9.26620513e-02, -9.81301889e-02,
            8.85765329e-02, -1.35882095e-01, -2.96573732e-02,
           -8.69414508e-02, -1.36860996e-01,  1.10277958e-01,
           -1.16895162e-01, -3.01348772e-02, -3.56807746e-02,
           -2.88332514e-02, -8.79679844e-02, -6.91774115e-02,
           -6.54565319e-02,  4.32563163e-02, -1.03569694e-01,
           -3.65044251e-02, -1.19461127e-01, -1.06849298e-01,
            3.11799198e-02,  8.01041164e-03,  6.45952150e-02,
            5.07725729e-03, -7.27380291e-02,  5.76203282e-04,
           -9.43952128e-02,  6.33790195e-02],
          [ 7.23442659e-02, -1.16933703e-01, -7.08830655e-02,
            1.43302888e-01, -9.03509781e-02,  1.87711809e-02,
            3.95100899e-02, -7.95123130e-02, -7.15579912e-02,
            3.93757634e-02,  1.92102194e-02,  1.39692768e-01,
            1.27679184e-01, -7.95098022e-02,  2.35838648e-02,
           -1.67648569e-02, -6.25412017e-02, -5.29011376e-02,
           -9.55979303e-02, -7.87201524e-02,  1.10680833e-01,
            2.18986142e-02,  4.30889279e-02,  2.50766464e-02,
           -2.51036547e-02,  1.20268650e-01,  1.40116721e-01,
           -6.86395168e-02, -6.61671162e-02,  1.49378907e-02,
            3.07935663e-02, -4.80928607e-02],
          [ 8.20267275e-02, -1.32199854e-01,  4.71820757e-02,
           -9.49374884e-02, -6.54998496e-02, -1.28471747e-01,
           -1.16396800e-01,  5.91114350e-02, -1.51923737e-02,
            1.35798886e-01,  1.08761594e-01, -6.06526434e-02,
           -5.75024001e-02,  2.40406487e-03,  5.63625172e-02,
           -5.33581302e-02,  9.66425091e-02,  3.85993794e-02,
           -3.49114463e-02,  3.55822220e-02,  9.17072967e-02,
            8.34178645e-03,  6.59331381e-02,  1.12199254e-01,
            1.24972582e-01,  1.16839088e-01,  2.05804985e-02,
            8.02329779e-02,  9.91903469e-02,  1.16153315e-01,
            6.90405294e-02,  2.42999699e-02]]],
 
 
        [[[-7.15790540e-02, -1.00682452e-01,  8.83406922e-02,
           -9.67424959e-02,  1.34046540e-01, -4.21659239e-02,
            6.12729043e-02,  2.65003480e-02, -6.51134700e-02,
            1.23923734e-01,  6.29064292e-02, -4.08344865e-02,
           -1.53426768e-03,  2.89141294e-03,  3.32898237e-02,
           -6.23746403e-02, -1.20500602e-01, -1.28398895e-01,
           -7.44921640e-02, -1.18555993e-01, -9.45926681e-02,
           -1.15623675e-01, -1.07180782e-01, -1.18955664e-01,
            3.74918506e-02, -1.03779599e-01, -4.83348779e-02,
            4.94979462e-03,  1.02992721e-01,  7.53514245e-02,
            7.09796790e-03, -5.29060066e-02],
          [ 1.27654895e-01,  9.49089825e-02, -2.99589988e-02,
           -1.15903735e-01, -4.72669825e-02,  1.11813039e-01,
            9.26682800e-02,  1.19093329e-01, -1.73512176e-02,
            8.20648894e-02, -9.12115127e-02, -1.08141638e-01,
            5.44297472e-02,  1.31863818e-01,  1.25134103e-02,
           -1.11333497e-01,  2.36399360e-02,  1.21612616e-01,
            1.09695112e-02, -1.31089136e-01,  4.89432476e-02,
            9.07744020e-02,  2.63530686e-02, -1.24311700e-01,
            5.79346418e-02, -6.38309270e-02, -4.20988798e-02,
           -1.05967432e-01, -1.08026505e-01,  4.58757207e-02,
           -4.27404642e-02, -8.13715085e-02],
          [-2.12533064e-02,  1.88454166e-02, -2.12116074e-02,
            5.93953989e-02, -4.42860387e-02, -1.61235314e-02,
           -1.14032425e-01, -3.43756229e-02, -6.21404313e-02,
           -1.15213081e-01,  6.91293553e-02,  1.02734849e-01,
            1.30064189e-01,  1.26904681e-01,  8.69676471e-02,
            5.90369962e-02,  2.34206282e-02,  6.63922206e-02,
            2.72663031e-02,  1.96229592e-02,  4.12887037e-02,
            1.18534371e-01, -4.33701538e-02,  7.14327544e-02,
           -1.55002670e-03, -4.09463197e-02,  3.13265100e-02,
           -3.36100757e-02,  8.83268192e-02,  4.44691144e-02,
            9.37696323e-02, -5.40882647e-02]],
 
         [[ 7.41360784e-02, -1.97689533e-02,  1.18319327e-02,
           -3.37429978e-02, -2.31307629e-03,  3.81260701e-02,
           -7.27221444e-02,  1.41971009e-02, -2.22447105e-02,
            1.00930110e-01,  1.33911043e-01, -1.12828009e-01,
           -6.01240657e-02, -1.15336597e-01, -4.50617224e-02,
            7.08505362e-02, -8.58158171e-02, -9.43364799e-02,
            9.92380455e-03, -5.25685474e-02, -1.33241475e-01,
            1.23824038e-01,  2.29803938e-02,  1.31789044e-01,
            9.48776454e-02, -2.90341005e-02, -1.06249839e-01,
            5.14587667e-03,  1.14899769e-01, -9.40244570e-02,
            1.07822359e-01,  7.04690441e-02],
          [-6.43399656e-02,  4.11047898e-02, -1.00507803e-01,
           -2.28294767e-02, -3.01104481e-03,  5.01529034e-03,
            1.29785508e-01, -7.74793327e-02, -1.08155742e-01,
           -1.02858290e-01, -1.16970740e-01, -8.11810568e-02,
           -4.76015210e-02, -3.42004895e-02, -1.81719046e-02,
            8.91719013e-02,  1.10170245e-01,  1.24142379e-01,
           -9.37316418e-02,  9.61344466e-02,  7.88844377e-03,
            2.03298759e-02,  5.97700253e-02, -6.95626959e-02,
            4.22742143e-02, -7.28302822e-02, -1.03519030e-01,
            1.75728649e-02,  8.74222741e-02,  2.57643498e-03,
            5.12361638e-02, -6.63342923e-02],
          [-1.28764138e-01,  7.18673021e-02,  8.12206864e-02,
            4.25383188e-02, -4.90712710e-02,  5.22668399e-02,
           -6.77991733e-02, -3.64914048e-03,  1.14887990e-01,
            6.31306097e-02, -9.82510857e-03, -2.01447215e-02,
           -1.33108690e-01, -9.62544084e-02, -6.96566552e-02,
           -2.65001617e-02, -9.34262499e-02, -1.18940793e-01,
           -6.81611076e-02, -9.50894952e-02, -1.31004348e-01,
           -9.88495871e-02, -9.86349653e-04, -5.19705638e-02,
           -7.17565641e-02,  5.16603813e-02,  1.04105458e-01,
            6.26074849e-03,  7.08314106e-02, -4.71091941e-02,
            6.32701740e-02, -7.30101243e-02]],
 
         [[ 9.38694328e-02,  6.44183755e-02,  6.44337684e-02,
           -7.84851685e-02,  1.11790918e-01, -3.76057927e-03,
           -8.52940455e-02,  1.06915355e-01,  7.86822382e-03,
           -1.21250242e-01,  1.50173428e-02, -8.59006271e-02,
            6.62351251e-02, -1.36470102e-04, -5.96383736e-02,
            8.12303573e-02, -1.00717209e-01,  9.19403788e-03,
            8.14287215e-02,  1.21296614e-01, -1.97709929e-02,
            1.14446685e-01, -1.26295688e-03,  9.64560080e-03,
            1.52408872e-02, -8.80312100e-02,  1.07382622e-03,
           -9.51410681e-02,  4.61466573e-02, -1.12374043e-02,
            4.01510065e-03,  1.36833221e-01],
          [-5.40566780e-02,  1.06103092e-01,  1.27054855e-01,
            4.25962023e-02,  2.58534662e-02, -1.59932894e-03,
           -3.41925807e-02,  4.68911864e-02,  4.40660492e-02,
           -1.15766749e-01, -1.37199862e-02, -6.50179833e-02,
           -1.44624356e-02,  6.41598254e-02, -1.09184541e-01,
            2.75072418e-02,  1.08387083e-01,  1.22067034e-01,
           -1.05614476e-01, -3.81900296e-02,  1.22096598e-01,
           -9.18397084e-02,  1.38824582e-02,  4.47208099e-02,
           -3.93188260e-02, -1.32676378e-01,  9.79875997e-02,
            1.33561432e-01,  1.11884966e-01, -4.36082743e-02,
           -1.90761276e-02,  6.87053800e-02],
          [-5.95129207e-02, -1.14375614e-01,  8.70322436e-02,
            3.14192697e-02,  1.11614712e-01, -1.94472689e-02,
           -3.10436077e-02, -3.51633765e-02, -1.28098503e-01,
            5.60924225e-02, -5.52196838e-02, -7.30516016e-02,
           -9.33454186e-03, -1.11377522e-01, -5.66947982e-02,
           -5.54126054e-02, -2.76415292e-02,  4.67314832e-02,
            3.86394449e-02,  3.06546558e-02, -4.87835109e-02,
            1.20554611e-01, -3.59630138e-02, -1.21430278e-01,
            7.06175566e-02, -1.98729872e-03, -1.10791706e-01,
            1.24884717e-01, -1.32951602e-01,  4.71706018e-02,
           -8.21407437e-02, -9.37529206e-02]]]], dtype=float32),
 array([ 2.0789087e-03, -5.0910679e-03, -1.0053901e-03, -4.8076613e-03,
        -3.7543299e-03, -2.4023603e-04,  3.5381300e-04, -1.0004530e-05,
        -4.7591416e-04,  9.0887945e-04, -2.3358059e-03, -3.1151259e-03,
         3.6628421e-03, -5.9115482e-03, -2.6593858e-03, -7.0348894e-03,
        -4.6357955e-03,  2.1537188e-03, -2.7784172e-03, -4.4654338e-03,
        -1.5419087e-03, -5.9814588e-03,  3.9480018e-04, -4.4522455e-04,
        -2.7490291e-03, -2.5109164e-04, -4.2385031e-03, -2.0953482e-03,
         1.4927351e-03, -2.9992920e-03, -5.8232211e-03, -6.6799570e-05],
       dtype=float32),
 array([[[[-0.00764071,  0.06933958, -0.00017186, ..., -0.03796613,
           -0.07325272,  0.00513361],
          [-0.04903064,  0.07813444,  0.02861374, ...,  0.00383972,
           -0.0804912 , -0.08354907],
          [-0.07936741,  0.00052877, -0.04164606, ..., -0.0589419 ,
           -0.02215986, -0.05868025],
          ...,
          [ 0.08161323,  0.00377183, -0.01764081, ..., -0.03055253,
            0.02119492, -0.07447971],
          [-0.00040037,  0.05587449,  0.07132279, ..., -0.03637168,
           -0.02679993, -0.0067021 ],
          [ 0.06326254, -0.0813743 , -0.01108303, ...,  0.07964972,
            0.0065103 , -0.06990585]],
 
         [[-0.03357387, -0.03134136, -0.02764487, ...,  0.07770841,
           -0.03064664,  0.07321124],
          [-0.01239829, -0.06108739, -0.08092728, ..., -0.04908804,
           -0.03612114, -0.01690319],
          [ 0.06191694, -0.06015697,  0.02015737, ..., -0.07720876,
           -0.06315303,  0.00890124],
          ...,
          [-0.05628914,  0.03166933,  0.04833107, ..., -0.00916554,
           -0.02244817,  0.05459451],
          [ 0.04600835,  0.02362875,  0.01362444, ..., -0.06534062,
           -0.07148145, -0.03359282],
          [ 0.01958497,  0.01303807, -0.00612756, ..., -0.0773375 ,
           -0.08005709,  0.04233177]],
 
         [[-0.04197074,  0.00354107,  0.05343959, ...,  0.07742179,
            0.04662604,  0.06372709],
          [-0.0662117 , -0.07091659, -0.03704059, ..., -0.00179729,
            0.02136324, -0.01950434],
          [ 0.08085931, -0.04248534, -0.00316317, ..., -0.08212123,
           -0.02397788,  0.05134417],
          ...,
          [ 0.04699778,  0.04037232,  0.00133666, ...,  0.01141372,
           -0.06814396,  0.02047676],
          [-0.04771322,  0.00062934, -0.04467395, ..., -0.06336714,
           -0.07083625,  0.00185384],
          [-0.03063972, -0.03012196, -0.03545046, ...,  0.03225502,
            0.02356039, -0.07868953]]],
 
 
        [[[ 0.02603668,  0.03160114, -0.06424306, ...,  0.02365624,
           -0.05000142,  0.00116366],
          [-0.01466135, -0.00660909, -0.0692841 , ...,  0.01041579,
            0.01011942,  0.06203382],
          [ 0.01926293, -0.05426513, -0.02882874, ..., -0.04975289,
            0.00195527,  0.00102711],
          ...,
          [ 0.07422502, -0.07568587, -0.04435132, ...,  0.03311471,
            0.04303723, -0.07900828],
          [-0.03237529, -0.01468308,  0.07078438, ..., -0.00812935,
           -0.03445014,  0.00402395],
          [-0.04606767,  0.00476294,  0.00091457, ..., -0.06081394,
            0.02625239,  0.05582291]],
 
         [[ 0.07129233,  0.00351365,  0.01572635, ..., -0.02348929,
            0.06236348,  0.02664841],
          [ 0.05280333, -0.01961258,  0.05594382, ..., -0.05011322,
           -0.06931012,  0.03598238],
          [-0.07209518,  0.01925111, -0.0671293 , ..., -0.0453678 ,
            0.02111903,  0.04087549],
          ...,
          [-0.01790602, -0.07110666,  0.03646073, ...,  0.00915694,
           -0.03867307, -0.02884112],
          [-0.08138271, -0.05406759, -0.07736579, ..., -0.04198918,
            0.02021008,  0.05236049],
          [ 0.03898291,  0.04933243,  0.01266384, ...,  0.05576876,
            0.07180185, -0.06442171]],
 
         [[ 0.0412758 , -0.0428097 ,  0.07572449, ..., -0.01072849,
           -0.06288777,  0.06500602],
          [-0.06721646, -0.03336762, -0.08925208, ..., -0.03776856,
           -0.055474  ,  0.00015181],
          [-0.0714833 , -0.05262364,  0.00015602, ...,  0.01836082,
            0.01721053, -0.01655661],
          ...,
          [ 0.07695182,  0.03591787,  0.06340532, ..., -0.07624332,
            0.00371815,  0.01195629],
          [-0.0694984 ,  0.0621696 ,  0.08156484, ...,  0.07139478,
           -0.04237859, -0.06122444],
          [ 0.02628231,  0.06179056,  0.05593789, ..., -0.06085656,
            0.01230763,  0.00293637]]],
 
 
        [[[-0.01213591, -0.05827904, -0.04775815, ..., -0.04171561,
           -0.04006168, -0.06309443],
          [-0.05645829,  0.01976245,  0.05733467, ...,  0.08058432,
            0.06871635, -0.01808074],
          [ 0.0060199 ,  0.03342728,  0.01298312, ..., -0.04545848,
            0.01932584,  0.05618505],
          ...,
          [-0.05685085,  0.0693889 , -0.07187606, ...,  0.01233756,
            0.00680015,  0.01939627],
          [-0.06450137,  0.07661791, -0.07585605, ...,  0.02138199,
           -0.06314614, -0.03240677],
          [ 0.04526063,  0.02131679,  0.00238692, ...,  0.0656295 ,
           -0.04304691, -0.07128759]],
 
         [[ 0.0819694 , -0.02980562,  0.06733992, ..., -0.01582912,
           -0.01627529, -0.0325886 ],
          [-0.02638585,  0.06366917,  0.00822976, ...,  0.07022822,
           -0.0387554 , -0.01877219],
          [ 0.01774235, -0.02330656, -0.05505393, ...,  0.0139383 ,
           -0.05393478, -0.07967435],
          ...,
          [ 0.03466257, -0.00792843, -0.07613721, ..., -0.05830956,
            0.03494347, -0.04413251],
          [-0.05021612, -0.0322809 ,  0.02051199, ..., -0.00718085,
           -0.07063309,  0.02845437],
          [-0.01503162, -0.00132979, -0.07882797, ..., -0.00952362,
            0.01332161, -0.02686104]],
 
         [[ 0.03412039,  0.00385929, -0.05941189, ...,  0.08220446,
           -0.03530798, -0.00056292],
          [ 0.02039827, -0.04597186, -0.06849087, ..., -0.07510023,
            0.06621467, -0.02544456],
          [ 0.07838272,  0.06708939, -0.05264354, ..., -0.02441663,
           -0.0581378 , -0.05969406],
          ...,
          [ 0.00670352, -0.03311561, -0.0575454 , ..., -0.06847577,
            0.05498725, -0.06131184],
          [ 0.04684874,  0.06291398, -0.01883244, ...,  0.0602392 ,
            0.07347226, -0.00815635],
          [-0.07705721,  0.03919682, -0.02696306, ..., -0.08352756,
            0.04336645, -0.01036202]]]], dtype=float32),
 array([ 1.6710028e-03, -7.3205568e-03, -5.9828879e-03, -5.2209906e-03,
        -5.4071425e-03, -4.8488067e-03, -5.6253588e-03, -3.6867510e-03,
        -6.0245958e-03, -4.8062066e-03, -3.1569882e-03, -3.8634513e-03,
        -2.5937797e-03,  2.2671074e-03, -2.4902937e-03, -1.5078248e-03,
        -8.4172585e-04, -4.9147485e-03,  9.6427964e-04, -3.4379181e-03,
        -7.2776899e-03, -9.5513515e-04, -5.8901669e-03,  2.3820241e-05,
        -2.3771808e-03, -5.1032393e-03, -4.1034860e-03, -4.5373463e-03,
        -3.7763235e-03, -4.5527774e-03, -1.7941892e-03, -4.1108965e-03,
        -1.4052001e-03,  1.6392984e-03, -3.7708229e-03, -5.3877174e-03,
        -2.2104138e-03, -1.9496641e-03,  1.1300127e-03, -2.7386877e-03,
        -1.1464375e-03, -2.9969078e-03, -1.6721602e-03, -1.1936557e-03,
        -4.0697595e-04, -2.5357371e-03, -2.8463097e-03, -6.9522359e-03,
        -4.2122980e-03, -3.5685385e-03, -3.4506144e-03, -7.2793628e-04,
        -5.2885334e-03, -5.9328736e-03,  2.4510515e-03, -5.2328571e-03,
        -3.5334481e-03, -2.8582586e-03, -1.8528446e-03, -2.3797287e-03,
        -4.2491588e-03, -1.2589484e-03,  3.2095073e-04, -7.0376825e-03],
       dtype=float32),
 array([[ 4.0688990e-03,  2.0415146e-05],
        [ 3.8258152e-03,  3.1568981e-03],
        [-4.5193387e-03,  1.8756374e-04],
        ...,
        [-5.9574214e-03,  1.4380299e-03],
        [ 3.5465364e-03, -4.3049110e-03],
        [-4.5696758e-03,  3.5738121e-05]], dtype=float32),
 array([ 0.00060867, -0.00060867], dtype=float32)]
In [46]:
new_model.optimizer
Out[46]:
<tensorflow.python.keras.optimizer_v2.adam.Adam at 0x18e25d13dc8>

2. model.to_json()

if you only need to save architecture of a model, and not its weights or its training configuration, you can see the following function to save the architecture only.

In [47]:
# save as YAML
yaml_string = model.to_yaml()

# save as JSON
# json_string = model.to_json()
In [48]:
yaml_string
Out[48]:
'backend: tensorflow\nclass_name: Sequential\nconfig:\n  layers:\n  - class_name: Conv2D\n    config:\n      activation: relu\n      activity_regularizer: null\n      batch_input_shape: !!python/tuple\n      - null\n      - 224\n      - 224\n      - 3\n      bias_constraint: null\n      bias_initializer:\n        class_name: Zeros\n        config:\n          dtype: float32\n      bias_regularizer: null\n      data_format: channels_last\n      dilation_rate: &id001 !!python/tuple\n      - 1\n      - 1\n      dtype: float32\n      filters: 32\n      kernel_constraint: null\n      kernel_initializer:\n        class_name: GlorotUniform\n        config:\n          dtype: float32\n          seed: null\n      kernel_regularizer: null\n      kernel_size: &id002 !!python/tuple\n      - 3\n      - 3\n      name: conv2d\n      padding: same\n      strides: *id001\n      trainable: true\n      use_bias: true\n  - class_name: MaxPooling2D\n    config:\n      data_format: channels_last\n      dtype: float32\n      name: max_pooling2d\n      padding: valid\n      pool_size: &id003 !!python/tuple\n      - 2\n      - 2\n      strides: !!python/tuple\n      - 2\n      - 2\n      trainable: true\n  - class_name: Conv2D\n    config:\n      activation: relu\n      activity_regularizer: null\n      bias_constraint: null\n      bias_initializer:\n        class_name: Zeros\n        config:\n          dtype: float32\n      bias_regularizer: null\n      data_format: channels_last\n      dilation_rate: *id001\n      dtype: float32\n      filters: 64\n      kernel_constraint: null\n      kernel_initializer:\n        class_name: GlorotUniform\n        config:\n          dtype: float32\n          seed: null\n      kernel_regularizer: null\n      kernel_size: *id002\n      name: conv2d_1\n      padding: same\n      strides: *id001\n      trainable: true\n      use_bias: true\n  - class_name: MaxPooling2D\n    config:\n      data_format: channels_last\n      dtype: float32\n      name: max_pooling2d_1\n      padding: valid\n      pool_size: *id003\n      strides: !!python/tuple\n      - 2\n      - 2\n      trainable: true\n  - class_name: Flatten\n    config:\n      data_format: channels_last\n      dtype: float32\n      name: flatten\n      trainable: true\n  - class_name: Dense\n    config:\n      activation: softmax\n      activity_regularizer: null\n      bias_constraint: null\n      bias_initializer:\n        class_name: Zeros\n        config:\n          dtype: float32\n      bias_regularizer: null\n      dtype: float32\n      kernel_constraint: null\n      kernel_initializer:\n        class_name: GlorotUniform\n        config:\n          dtype: float32\n          seed: null\n      kernel_regularizer: null\n      name: dense\n      trainable: true\n      units: 2\n      use_bias: true\n  name: sequential\nkeras_version: 2.2.4-tf\n'
In [49]:
# model reconstruction from YAML
from tensorflow.keras.models import model_from_yaml
model_architecture = model_from_yaml(yaml_string)

# model reconstruction from JSON:
# from tensorflow.keras.models import model_from_json
# model_architecture = model_from_json(json_string)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\saving\model_config.py:76: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
  config = yaml.load(yaml_string)
In [50]:
model_architecture.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 224, 224, 32)      896       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 112, 112, 32)      0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 112, 112, 64)      18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 56, 56, 64)        0         
_________________________________________________________________
flatten (Flatten)            (None, 200704)            0         
_________________________________________________________________
dense (Dense)                (None, 2)                 401410    
=================================================================
Total params: 420,802
Trainable params: 420,802
Non-trainable params: 0
_________________________________________________________________

3. model.save_weights()

If you only need to save the weights of the model, you cab use the following function save the weights only.

In [51]:
# Checks first to see if file exists already
# If not, the model is saved to disk.
import os.path
if os.path.isfile('C:\\Users\\רועי\\models\skincancer_model.h7') is False:
    model.save_weights('C:\\Users\\רועי\\models\skincancer_model.h7')
In [52]:
model2 = Sequential([
    Conv2D(filters=32, kernel_size=(3, 3), activation='relu', padding = 'same', input_shape=(224,224,3)),
    MaxPool2D(pool_size=(2, 2), strides=2),
    Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding = 'same'),
    MaxPool2D(pool_size=(2, 2), strides=2),
    Flatten(),
    Dense(units=2, activation='softmax')
])
In [53]:
model2.load_weights('C:\\Users\\רועי\\models\skincancer_model.h7')
In [54]:
model2.get_weights()
Out[54]:
[array([[[[ 1.38012096e-01, -8.84406865e-02,  1.20834507e-01,
            1.20329633e-01, -1.16766378e-01,  7.45493323e-02,
           -1.34152964e-01, -1.15893736e-01, -8.40075389e-02,
           -7.46510699e-02,  1.33424535e-01, -7.51701277e-03,
           -3.67834829e-02,  8.52866620e-02, -1.00812070e-01,
           -1.64543353e-02, -9.01955739e-03,  1.06778115e-01,
           -7.34757707e-02,  7.54573047e-02, -5.29317670e-02,
            9.08764452e-02, -1.27979338e-01, -3.71927186e-03,
           -2.38801409e-02, -1.02364317e-01, -1.08862691e-01,
            1.25361934e-01, -8.83691981e-02,  3.83781604e-02,
            7.98096731e-02, -1.16856664e-01],
          [ 6.22796603e-02, -7.23754466e-02, -9.91126373e-02,
            8.96538347e-02,  2.31015757e-02, -7.83761218e-02,
            2.62644459e-02,  1.14494003e-01,  9.14754421e-02,
            1.14326626e-02, -7.66148865e-02, -7.30372369e-02,
           -2.92360950e-02, -1.10828280e-01,  1.34554029e-01,
            4.18831706e-02,  7.84734264e-02,  6.00380450e-02,
            3.76507267e-02,  6.89542666e-02, -3.90463658e-02,
           -2.22085230e-02,  7.78661668e-02, -2.86889281e-02,
            5.52291125e-02,  1.38606355e-01, -1.17721699e-01,
            8.06322247e-02, -1.08877674e-01,  5.86248301e-02,
            3.91125344e-02, -6.37635123e-03],
          [ 3.61001119e-02, -1.27298340e-01,  1.30918235e-01,
           -1.31724015e-01,  9.87402648e-02,  3.85304652e-02,
           -7.65362382e-02, -6.56289235e-02, -1.11280508e-01,
            4.79274429e-02,  6.01455308e-02,  4.20850702e-02,
           -1.11730240e-01,  1.21583000e-01,  1.21500619e-01,
           -1.10844269e-01,  4.99703325e-02, -5.36879003e-02,
            8.25554430e-02,  1.33216411e-01,  4.82029915e-02,
           -3.65616940e-03,  2.61972640e-02, -5.84965274e-02,
           -5.09034283e-02, -6.61584884e-02, -8.41583759e-02,
           -1.76085159e-02,  9.63048041e-02,  2.90775560e-02,
           -9.25419182e-02,  2.50847749e-02]],
 
         [[-3.15915272e-02,  2.53626592e-02,  1.33855894e-01,
            2.09373999e-02,  6.36935234e-03,  3.22284326e-02,
            3.48305106e-02, -4.83667888e-02, -7.72036910e-02,
            8.96398202e-02, -7.23013952e-02,  5.20349555e-02,
            8.89291149e-03, -5.59077263e-02,  1.18802696e-01,
           -8.89856219e-02, -1.26309574e-01, -1.34811714e-01,
           -3.31439786e-02,  8.70612338e-02, -8.85880888e-02,
           -1.00435272e-01, -1.22203670e-01,  3.76036303e-04,
            1.24958288e-02, -8.77540112e-02, -1.06317505e-01,
            7.50828534e-03,  5.79860210e-02, -8.18044990e-02,
            9.27680079e-03,  3.20650339e-02],
          [ 5.66818984e-03,  9.60438251e-02,  1.03375025e-01,
           -6.91270158e-02, -1.13606259e-01, -1.03402346e-01,
            6.85153231e-02,  8.88056457e-02, -2.60776980e-03,
            2.01513823e-02, -5.19283079e-02,  1.24803662e-01,
            3.14342082e-02,  6.79740608e-02, -5.35403453e-02,
            8.64978433e-02,  3.15252990e-02, -1.17797509e-01,
            1.26365453e-01,  1.27379149e-01, -1.14447400e-01,
            7.25468621e-02, -6.65635914e-02,  7.70748034e-02,
            9.81387496e-02, -1.19082496e-01,  3.80955823e-02,
           -1.71924811e-02,  1.09196484e-01, -3.52888443e-02,
            1.22958906e-01, -5.22010997e-02],
          [ 2.16924120e-02,  4.22612652e-02,  1.20890569e-02,
            9.90278721e-02, -3.31160054e-02, -8.78909826e-02,
           -6.75626621e-02, -1.26639307e-01,  3.04759704e-02,
            1.12492248e-01,  2.66952836e-03,  2.58892328e-02,
            5.63528538e-02, -7.36360550e-02,  8.67929831e-02,
            6.73651546e-02,  4.20602523e-02,  1.28744513e-01,
           -9.33288112e-02,  4.83517163e-02, -4.72525917e-02,
            5.80399148e-02, -1.21989466e-01,  2.34387387e-02,
            8.17174837e-02, -1.22700319e-01, -3.91919874e-02,
           -6.88635111e-02, -4.14893106e-02,  3.90252955e-02,
            8.22026059e-02,  3.04137310e-03]],
 
         [[-1.13243967e-01, -3.03858742e-02, -6.74555302e-02,
           -1.39478836e-02,  9.55590904e-02,  4.94423844e-02,
            1.09593824e-01, -1.04166038e-01,  1.19421184e-01,
            1.33578241e-01,  1.34681180e-01, -1.29124016e-01,
            7.17445239e-02, -6.99853152e-02, -9.13952813e-02,
           -1.03688776e-01, -8.90705958e-02, -5.87549387e-03,
            1.12756640e-02,  8.68401229e-02,  1.16083793e-01,
           -6.10074438e-02, -6.23488938e-03, -9.02798325e-02,
           -1.39221489e-01,  4.75379862e-02,  9.52165946e-02,
            8.69382522e-04,  3.52889001e-02,  1.76504068e-02,
           -5.01421420e-03,  1.14564329e-01],
          [-1.18791334e-01,  3.26167345e-02,  9.21265706e-02,
           -5.66888489e-02, -1.18214086e-01,  1.17234543e-01,
           -6.53363690e-02,  1.35980666e-01,  6.83877841e-02,
           -3.59751135e-02, -2.26282962e-02,  3.10736746e-02,
            5.35915280e-03,  1.15953676e-01,  2.65577622e-02,
           -5.25063463e-02,  5.42227440e-02, -1.06632203e-01,
            2.31003687e-02, -1.29438881e-02,  1.05057798e-01,
            7.69168884e-02,  2.62694396e-02,  1.15889952e-01,
            6.47801161e-02, -1.29644290e-01,  8.36851299e-02,
            1.10428073e-02, -1.13662958e-01, -1.07159674e-01,
           -1.14953578e-01,  1.01206966e-01],
          [-4.43135127e-02,  7.92076513e-02, -5.78693040e-02,
           -7.15398788e-02,  4.26418781e-02,  7.95254111e-02,
           -1.54244145e-02,  7.77518824e-02,  1.19091749e-01,
           -1.09476142e-01, -4.59219851e-02, -9.14272442e-02,
           -1.38114691e-01, -5.66439070e-02,  1.20284326e-01,
           -1.31065682e-01, -1.06112145e-01,  1.02248743e-01,
           -4.86445948e-02, -1.31601304e-01, -2.96466034e-02,
           -1.15997784e-01,  7.95469135e-02, -1.05960675e-01,
            1.06837176e-01,  2.44174805e-02, -7.40103349e-02,
            7.50668496e-02, -6.00052737e-02,  3.80902295e-03,
            1.27229929e-01, -3.35594006e-02]]],
 
 
        [[[ 2.45169364e-03, -2.86237709e-03, -7.98791274e-03,
           -3.73667739e-02,  8.03174004e-02, -1.96691155e-02,
           -7.77103677e-02,  8.98281559e-02,  6.34301677e-02,
           -1.31189048e-01,  1.03952296e-01, -1.11763380e-01,
           -4.62341793e-02, -2.72931773e-02,  1.03552558e-01,
           -7.65201598e-02, -9.07593891e-02,  7.19395727e-02,
            1.85596962e-02,  1.29504250e-02,  9.46459398e-02,
            5.51584223e-03,  1.27091214e-01, -1.29904985e-01,
            5.59513867e-02,  5.19930013e-03, -1.87762864e-02,
           -7.96043873e-02, -1.23457998e-01,  7.20390752e-02,
            1.13124810e-01,  5.71398577e-03],
          [ 6.25795797e-02, -1.06990732e-01, -9.79946852e-02,
            6.59460016e-03, -8.50046203e-02, -8.58902559e-02,
            8.57231915e-02,  1.17119998e-01, -5.60670830e-02,
           -1.94190610e-02, -3.34095918e-02, -2.71843970e-02,
            2.18488853e-02, -9.40648392e-02, -1.90243274e-02,
            4.97210249e-02, -1.99519899e-02,  7.12958677e-03,
            4.99475449e-02,  1.19867943e-01, -8.87091309e-02,
           -1.38903588e-01,  5.06118033e-03, -5.03636375e-02,
           -6.19172826e-02, -1.91012323e-02,  1.34429395e-01,
           -1.01476595e-01, -4.62766513e-02, -4.67154644e-02,
           -6.39601785e-04,  1.18888162e-01],
          [-6.51676804e-02, -7.03894868e-02,  1.29924342e-01,
           -6.35272264e-02, -7.22595155e-02,  1.01845413e-01,
            1.24218494e-01, -7.11901784e-02, -5.47299124e-02,
            5.08526266e-02, -5.13649695e-02,  1.03991918e-01,
            8.51125494e-02, -7.28062689e-02, -1.24846175e-01,
            8.29203501e-02, -5.74660972e-02,  5.76177835e-02,
            1.16232425e-01,  8.45944136e-02,  1.04224242e-01,
            5.60795702e-02,  5.69354407e-02, -3.90475169e-02,
           -1.19932309e-01, -4.73300591e-02,  3.23001780e-02,
           -6.47194833e-02,  4.57348712e-02, -9.27224085e-02,
            6.17517084e-02,  9.22940075e-02]],
 
         [[ 2.97921933e-02, -8.22824463e-02, -4.17748047e-03,
            1.81095563e-02, -4.31207381e-02, -5.98638989e-02,
            2.99708098e-02, -1.87734980e-02, -9.46068019e-02,
            1.25313342e-01,  9.58209112e-02,  5.96114136e-02,
            5.12314178e-02, -1.05512291e-01,  9.73367542e-02,
           -9.00197849e-02, -2.69910377e-02,  5.38100861e-02,
            7.14058951e-02,  1.75184663e-02, -1.70577932e-02,
            1.77334137e-02,  9.67472047e-02,  1.08446158e-01,
           -1.32045284e-01,  9.20715779e-02, -1.28105298e-01,
            1.03573970e-01,  1.10896885e-01, -3.42792794e-02,
           -4.08363976e-02,  6.88628703e-02],
          [-1.87277514e-02, -1.51816308e-02, -1.36438102e-01,
           -4.25186427e-03, -9.71929803e-02, -1.01062413e-02,
           -7.59762526e-02,  1.15712196e-01, -4.07594116e-03,
            4.57760356e-02,  9.58429873e-02, -1.00841098e-01,
           -4.76827435e-02, -7.66278729e-02, -6.37250319e-02,
           -6.07004017e-02,  1.26912490e-01,  9.40351561e-02,
           -1.13452397e-01,  8.79292637e-02, -2.41072904e-02,
            8.87157843e-02, -3.79834585e-02, -3.73128578e-02,
           -1.16166994e-01,  6.98104650e-02, -4.75598779e-03,
            6.04438558e-02, -1.26588494e-01,  1.28325060e-01,
            4.31407653e-02, -1.32414192e-01],
          [-1.05637647e-01,  4.17511724e-02, -2.84398105e-02,
           -6.70498759e-02,  5.82432793e-03, -3.19725648e-02,
            1.20018758e-01,  7.82972202e-02, -8.03264230e-02,
            1.26096904e-01,  2.75085196e-02,  2.59983093e-02,
            8.83707777e-02,  9.00678933e-02,  1.12566382e-01,
            5.96420020e-02,  8.65440741e-02,  7.00573111e-03,
           -1.10285155e-01, -6.39790148e-02,  1.25691622e-01,
            1.24573223e-01,  1.07342936e-01, -2.40996126e-02,
            1.30076900e-01,  3.07643805e-02, -1.09452590e-01,
            5.82771143e-03,  1.21228993e-01, -1.24223426e-01,
            5.31139597e-02, -2.95768064e-02]],
 
         [[-1.14101872e-01,  1.30948722e-01,  3.64383347e-02,
           -9.63269621e-02,  9.26620513e-02, -9.81301889e-02,
            8.85765329e-02, -1.35882095e-01, -2.96573732e-02,
           -8.69414508e-02, -1.36860996e-01,  1.10277958e-01,
           -1.16895162e-01, -3.01348772e-02, -3.56807746e-02,
           -2.88332514e-02, -8.79679844e-02, -6.91774115e-02,
           -6.54565319e-02,  4.32563163e-02, -1.03569694e-01,
           -3.65044251e-02, -1.19461127e-01, -1.06849298e-01,
            3.11799198e-02,  8.01041164e-03,  6.45952150e-02,
            5.07725729e-03, -7.27380291e-02,  5.76203282e-04,
           -9.43952128e-02,  6.33790195e-02],
          [ 7.23442659e-02, -1.16933703e-01, -7.08830655e-02,
            1.43302888e-01, -9.03509781e-02,  1.87711809e-02,
            3.95100899e-02, -7.95123130e-02, -7.15579912e-02,
            3.93757634e-02,  1.92102194e-02,  1.39692768e-01,
            1.27679184e-01, -7.95098022e-02,  2.35838648e-02,
           -1.67648569e-02, -6.25412017e-02, -5.29011376e-02,
           -9.55979303e-02, -7.87201524e-02,  1.10680833e-01,
            2.18986142e-02,  4.30889279e-02,  2.50766464e-02,
           -2.51036547e-02,  1.20268650e-01,  1.40116721e-01,
           -6.86395168e-02, -6.61671162e-02,  1.49378907e-02,
            3.07935663e-02, -4.80928607e-02],
          [ 8.20267275e-02, -1.32199854e-01,  4.71820757e-02,
           -9.49374884e-02, -6.54998496e-02, -1.28471747e-01,
           -1.16396800e-01,  5.91114350e-02, -1.51923737e-02,
            1.35798886e-01,  1.08761594e-01, -6.06526434e-02,
           -5.75024001e-02,  2.40406487e-03,  5.63625172e-02,
           -5.33581302e-02,  9.66425091e-02,  3.85993794e-02,
           -3.49114463e-02,  3.55822220e-02,  9.17072967e-02,
            8.34178645e-03,  6.59331381e-02,  1.12199254e-01,
            1.24972582e-01,  1.16839088e-01,  2.05804985e-02,
            8.02329779e-02,  9.91903469e-02,  1.16153315e-01,
            6.90405294e-02,  2.42999699e-02]]],
 
 
        [[[-7.15790540e-02, -1.00682452e-01,  8.83406922e-02,
           -9.67424959e-02,  1.34046540e-01, -4.21659239e-02,
            6.12729043e-02,  2.65003480e-02, -6.51134700e-02,
            1.23923734e-01,  6.29064292e-02, -4.08344865e-02,
           -1.53426768e-03,  2.89141294e-03,  3.32898237e-02,
           -6.23746403e-02, -1.20500602e-01, -1.28398895e-01,
           -7.44921640e-02, -1.18555993e-01, -9.45926681e-02,
           -1.15623675e-01, -1.07180782e-01, -1.18955664e-01,
            3.74918506e-02, -1.03779599e-01, -4.83348779e-02,
            4.94979462e-03,  1.02992721e-01,  7.53514245e-02,
            7.09796790e-03, -5.29060066e-02],
          [ 1.27654895e-01,  9.49089825e-02, -2.99589988e-02,
           -1.15903735e-01, -4.72669825e-02,  1.11813039e-01,
            9.26682800e-02,  1.19093329e-01, -1.73512176e-02,
            8.20648894e-02, -9.12115127e-02, -1.08141638e-01,
            5.44297472e-02,  1.31863818e-01,  1.25134103e-02,
           -1.11333497e-01,  2.36399360e-02,  1.21612616e-01,
            1.09695112e-02, -1.31089136e-01,  4.89432476e-02,
            9.07744020e-02,  2.63530686e-02, -1.24311700e-01,
            5.79346418e-02, -6.38309270e-02, -4.20988798e-02,
           -1.05967432e-01, -1.08026505e-01,  4.58757207e-02,
           -4.27404642e-02, -8.13715085e-02],
          [-2.12533064e-02,  1.88454166e-02, -2.12116074e-02,
            5.93953989e-02, -4.42860387e-02, -1.61235314e-02,
           -1.14032425e-01, -3.43756229e-02, -6.21404313e-02,
           -1.15213081e-01,  6.91293553e-02,  1.02734849e-01,
            1.30064189e-01,  1.26904681e-01,  8.69676471e-02,
            5.90369962e-02,  2.34206282e-02,  6.63922206e-02,
            2.72663031e-02,  1.96229592e-02,  4.12887037e-02,
            1.18534371e-01, -4.33701538e-02,  7.14327544e-02,
           -1.55002670e-03, -4.09463197e-02,  3.13265100e-02,
           -3.36100757e-02,  8.83268192e-02,  4.44691144e-02,
            9.37696323e-02, -5.40882647e-02]],
 
         [[ 7.41360784e-02, -1.97689533e-02,  1.18319327e-02,
           -3.37429978e-02, -2.31307629e-03,  3.81260701e-02,
           -7.27221444e-02,  1.41971009e-02, -2.22447105e-02,
            1.00930110e-01,  1.33911043e-01, -1.12828009e-01,
           -6.01240657e-02, -1.15336597e-01, -4.50617224e-02,
            7.08505362e-02, -8.58158171e-02, -9.43364799e-02,
            9.92380455e-03, -5.25685474e-02, -1.33241475e-01,
            1.23824038e-01,  2.29803938e-02,  1.31789044e-01,
            9.48776454e-02, -2.90341005e-02, -1.06249839e-01,
            5.14587667e-03,  1.14899769e-01, -9.40244570e-02,
            1.07822359e-01,  7.04690441e-02],
          [-6.43399656e-02,  4.11047898e-02, -1.00507803e-01,
           -2.28294767e-02, -3.01104481e-03,  5.01529034e-03,
            1.29785508e-01, -7.74793327e-02, -1.08155742e-01,
           -1.02858290e-01, -1.16970740e-01, -8.11810568e-02,
           -4.76015210e-02, -3.42004895e-02, -1.81719046e-02,
            8.91719013e-02,  1.10170245e-01,  1.24142379e-01,
           -9.37316418e-02,  9.61344466e-02,  7.88844377e-03,
            2.03298759e-02,  5.97700253e-02, -6.95626959e-02,
            4.22742143e-02, -7.28302822e-02, -1.03519030e-01,
            1.75728649e-02,  8.74222741e-02,  2.57643498e-03,
            5.12361638e-02, -6.63342923e-02],
          [-1.28764138e-01,  7.18673021e-02,  8.12206864e-02,
            4.25383188e-02, -4.90712710e-02,  5.22668399e-02,
           -6.77991733e-02, -3.64914048e-03,  1.14887990e-01,
            6.31306097e-02, -9.82510857e-03, -2.01447215e-02,
           -1.33108690e-01, -9.62544084e-02, -6.96566552e-02,
           -2.65001617e-02, -9.34262499e-02, -1.18940793e-01,
           -6.81611076e-02, -9.50894952e-02, -1.31004348e-01,
           -9.88495871e-02, -9.86349653e-04, -5.19705638e-02,
           -7.17565641e-02,  5.16603813e-02,  1.04105458e-01,
            6.26074849e-03,  7.08314106e-02, -4.71091941e-02,
            6.32701740e-02, -7.30101243e-02]],
 
         [[ 9.38694328e-02,  6.44183755e-02,  6.44337684e-02,
           -7.84851685e-02,  1.11790918e-01, -3.76057927e-03,
           -8.52940455e-02,  1.06915355e-01,  7.86822382e-03,
           -1.21250242e-01,  1.50173428e-02, -8.59006271e-02,
            6.62351251e-02, -1.36470102e-04, -5.96383736e-02,
            8.12303573e-02, -1.00717209e-01,  9.19403788e-03,
            8.14287215e-02,  1.21296614e-01, -1.97709929e-02,
            1.14446685e-01, -1.26295688e-03,  9.64560080e-03,
            1.52408872e-02, -8.80312100e-02,  1.07382622e-03,
           -9.51410681e-02,  4.61466573e-02, -1.12374043e-02,
            4.01510065e-03,  1.36833221e-01],
          [-5.40566780e-02,  1.06103092e-01,  1.27054855e-01,
            4.25962023e-02,  2.58534662e-02, -1.59932894e-03,
           -3.41925807e-02,  4.68911864e-02,  4.40660492e-02,
           -1.15766749e-01, -1.37199862e-02, -6.50179833e-02,
           -1.44624356e-02,  6.41598254e-02, -1.09184541e-01,
            2.75072418e-02,  1.08387083e-01,  1.22067034e-01,
           -1.05614476e-01, -3.81900296e-02,  1.22096598e-01,
           -9.18397084e-02,  1.38824582e-02,  4.47208099e-02,
           -3.93188260e-02, -1.32676378e-01,  9.79875997e-02,
            1.33561432e-01,  1.11884966e-01, -4.36082743e-02,
           -1.90761276e-02,  6.87053800e-02],
          [-5.95129207e-02, -1.14375614e-01,  8.70322436e-02,
            3.14192697e-02,  1.11614712e-01, -1.94472689e-02,
           -3.10436077e-02, -3.51633765e-02, -1.28098503e-01,
            5.60924225e-02, -5.52196838e-02, -7.30516016e-02,
           -9.33454186e-03, -1.11377522e-01, -5.66947982e-02,
           -5.54126054e-02, -2.76415292e-02,  4.67314832e-02,
            3.86394449e-02,  3.06546558e-02, -4.87835109e-02,
            1.20554611e-01, -3.59630138e-02, -1.21430278e-01,
            7.06175566e-02, -1.98729872e-03, -1.10791706e-01,
            1.24884717e-01, -1.32951602e-01,  4.71706018e-02,
           -8.21407437e-02, -9.37529206e-02]]]], dtype=float32),
 array([ 2.0789087e-03, -5.0910679e-03, -1.0053901e-03, -4.8076613e-03,
        -3.7543299e-03, -2.4023603e-04,  3.5381300e-04, -1.0004530e-05,
        -4.7591416e-04,  9.0887945e-04, -2.3358059e-03, -3.1151259e-03,
         3.6628421e-03, -5.9115482e-03, -2.6593858e-03, -7.0348894e-03,
        -4.6357955e-03,  2.1537188e-03, -2.7784172e-03, -4.4654338e-03,
        -1.5419087e-03, -5.9814588e-03,  3.9480018e-04, -4.4522455e-04,
        -2.7490291e-03, -2.5109164e-04, -4.2385031e-03, -2.0953482e-03,
         1.4927351e-03, -2.9992920e-03, -5.8232211e-03, -6.6799570e-05],
       dtype=float32),
 array([[[[-0.00764071,  0.06933958, -0.00017186, ..., -0.03796613,
           -0.07325272,  0.00513361],
          [-0.04903064,  0.07813444,  0.02861374, ...,  0.00383972,
           -0.0804912 , -0.08354907],
          [-0.07936741,  0.00052877, -0.04164606, ..., -0.0589419 ,
           -0.02215986, -0.05868025],
          ...,
          [ 0.08161323,  0.00377183, -0.01764081, ..., -0.03055253,
            0.02119492, -0.07447971],
          [-0.00040037,  0.05587449,  0.07132279, ..., -0.03637168,
           -0.02679993, -0.0067021 ],
          [ 0.06326254, -0.0813743 , -0.01108303, ...,  0.07964972,
            0.0065103 , -0.06990585]],
 
         [[-0.03357387, -0.03134136, -0.02764487, ...,  0.07770841,
           -0.03064664,  0.07321124],
          [-0.01239829, -0.06108739, -0.08092728, ..., -0.04908804,
           -0.03612114, -0.01690319],
          [ 0.06191694, -0.06015697,  0.02015737, ..., -0.07720876,
           -0.06315303,  0.00890124],
          ...,
          [-0.05628914,  0.03166933,  0.04833107, ..., -0.00916554,
           -0.02244817,  0.05459451],
          [ 0.04600835,  0.02362875,  0.01362444, ..., -0.06534062,
           -0.07148145, -0.03359282],
          [ 0.01958497,  0.01303807, -0.00612756, ..., -0.0773375 ,
           -0.08005709,  0.04233177]],
 
         [[-0.04197074,  0.00354107,  0.05343959, ...,  0.07742179,
            0.04662604,  0.06372709],
          [-0.0662117 , -0.07091659, -0.03704059, ..., -0.00179729,
            0.02136324, -0.01950434],
          [ 0.08085931, -0.04248534, -0.00316317, ..., -0.08212123,
           -0.02397788,  0.05134417],
          ...,
          [ 0.04699778,  0.04037232,  0.00133666, ...,  0.01141372,
           -0.06814396,  0.02047676],
          [-0.04771322,  0.00062934, -0.04467395, ..., -0.06336714,
           -0.07083625,  0.00185384],
          [-0.03063972, -0.03012196, -0.03545046, ...,  0.03225502,
            0.02356039, -0.07868953]]],
 
 
        [[[ 0.02603668,  0.03160114, -0.06424306, ...,  0.02365624,
           -0.05000142,  0.00116366],
          [-0.01466135, -0.00660909, -0.0692841 , ...,  0.01041579,
            0.01011942,  0.06203382],
          [ 0.01926293, -0.05426513, -0.02882874, ..., -0.04975289,
            0.00195527,  0.00102711],
          ...,
          [ 0.07422502, -0.07568587, -0.04435132, ...,  0.03311471,
            0.04303723, -0.07900828],
          [-0.03237529, -0.01468308,  0.07078438, ..., -0.00812935,
           -0.03445014,  0.00402395],
          [-0.04606767,  0.00476294,  0.00091457, ..., -0.06081394,
            0.02625239,  0.05582291]],
 
         [[ 0.07129233,  0.00351365,  0.01572635, ..., -0.02348929,
            0.06236348,  0.02664841],
          [ 0.05280333, -0.01961258,  0.05594382, ..., -0.05011322,
           -0.06931012,  0.03598238],
          [-0.07209518,  0.01925111, -0.0671293 , ..., -0.0453678 ,
            0.02111903,  0.04087549],
          ...,
          [-0.01790602, -0.07110666,  0.03646073, ...,  0.00915694,
           -0.03867307, -0.02884112],
          [-0.08138271, -0.05406759, -0.07736579, ..., -0.04198918,
            0.02021008,  0.05236049],
          [ 0.03898291,  0.04933243,  0.01266384, ...,  0.05576876,
            0.07180185, -0.06442171]],
 
         [[ 0.0412758 , -0.0428097 ,  0.07572449, ..., -0.01072849,
           -0.06288777,  0.06500602],
          [-0.06721646, -0.03336762, -0.08925208, ..., -0.03776856,
           -0.055474  ,  0.00015181],
          [-0.0714833 , -0.05262364,  0.00015602, ...,  0.01836082,
            0.01721053, -0.01655661],
          ...,
          [ 0.07695182,  0.03591787,  0.06340532, ..., -0.07624332,
            0.00371815,  0.01195629],
          [-0.0694984 ,  0.0621696 ,  0.08156484, ...,  0.07139478,
           -0.04237859, -0.06122444],
          [ 0.02628231,  0.06179056,  0.05593789, ..., -0.06085656,
            0.01230763,  0.00293637]]],
 
 
        [[[-0.01213591, -0.05827904, -0.04775815, ..., -0.04171561,
           -0.04006168, -0.06309443],
          [-0.05645829,  0.01976245,  0.05733467, ...,  0.08058432,
            0.06871635, -0.01808074],
          [ 0.0060199 ,  0.03342728,  0.01298312, ..., -0.04545848,
            0.01932584,  0.05618505],
          ...,
          [-0.05685085,  0.0693889 , -0.07187606, ...,  0.01233756,
            0.00680015,  0.01939627],
          [-0.06450137,  0.07661791, -0.07585605, ...,  0.02138199,
           -0.06314614, -0.03240677],
          [ 0.04526063,  0.02131679,  0.00238692, ...,  0.0656295 ,
           -0.04304691, -0.07128759]],
 
         [[ 0.0819694 , -0.02980562,  0.06733992, ..., -0.01582912,
           -0.01627529, -0.0325886 ],
          [-0.02638585,  0.06366917,  0.00822976, ...,  0.07022822,
           -0.0387554 , -0.01877219],
          [ 0.01774235, -0.02330656, -0.05505393, ...,  0.0139383 ,
           -0.05393478, -0.07967435],
          ...,
          [ 0.03466257, -0.00792843, -0.07613721, ..., -0.05830956,
            0.03494347, -0.04413251],
          [-0.05021612, -0.0322809 ,  0.02051199, ..., -0.00718085,
           -0.07063309,  0.02845437],
          [-0.01503162, -0.00132979, -0.07882797, ..., -0.00952362,
            0.01332161, -0.02686104]],
 
         [[ 0.03412039,  0.00385929, -0.05941189, ...,  0.08220446,
           -0.03530798, -0.00056292],
          [ 0.02039827, -0.04597186, -0.06849087, ..., -0.07510023,
            0.06621467, -0.02544456],
          [ 0.07838272,  0.06708939, -0.05264354, ..., -0.02441663,
           -0.0581378 , -0.05969406],
          ...,
          [ 0.00670352, -0.03311561, -0.0575454 , ..., -0.06847577,
            0.05498725, -0.06131184],
          [ 0.04684874,  0.06291398, -0.01883244, ...,  0.0602392 ,
            0.07347226, -0.00815635],
          [-0.07705721,  0.03919682, -0.02696306, ..., -0.08352756,
            0.04336645, -0.01036202]]]], dtype=float32),
 array([ 1.6710028e-03, -7.3205568e-03, -5.9828879e-03, -5.2209906e-03,
        -5.4071425e-03, -4.8488067e-03, -5.6253588e-03, -3.6867510e-03,
        -6.0245958e-03, -4.8062066e-03, -3.1569882e-03, -3.8634513e-03,
        -2.5937797e-03,  2.2671074e-03, -2.4902937e-03, -1.5078248e-03,
        -8.4172585e-04, -4.9147485e-03,  9.6427964e-04, -3.4379181e-03,
        -7.2776899e-03, -9.5513515e-04, -5.8901669e-03,  2.3820241e-05,
        -2.3771808e-03, -5.1032393e-03, -4.1034860e-03, -4.5373463e-03,
        -3.7763235e-03, -4.5527774e-03, -1.7941892e-03, -4.1108965e-03,
        -1.4052001e-03,  1.6392984e-03, -3.7708229e-03, -5.3877174e-03,
        -2.2104138e-03, -1.9496641e-03,  1.1300127e-03, -2.7386877e-03,
        -1.1464375e-03, -2.9969078e-03, -1.6721602e-03, -1.1936557e-03,
        -4.0697595e-04, -2.5357371e-03, -2.8463097e-03, -6.9522359e-03,
        -4.2122980e-03, -3.5685385e-03, -3.4506144e-03, -7.2793628e-04,
        -5.2885334e-03, -5.9328736e-03,  2.4510515e-03, -5.2328571e-03,
        -3.5334481e-03, -2.8582586e-03, -1.8528446e-03, -2.3797287e-03,
        -4.2491588e-03, -1.2589484e-03,  3.2095073e-04, -7.0376825e-03],
       dtype=float32),
 array([[ 4.0688990e-03,  2.0415146e-05],
        [ 3.8258152e-03,  3.1568981e-03],
        [-4.5193387e-03,  1.8756374e-04],
        ...,
        [-5.9574214e-03,  1.4380299e-03],
        [ 3.5465364e-03, -4.3049110e-03],
        [-4.5696758e-03,  3.5738121e-05]], dtype=float32),
 array([ 0.00060867, -0.00060867], dtype=float32)]

10. Clean and Delete all the Directories

In [55]:
# Delete data from train directories
os.chdir('C:\\Users\\רועי\\data\\skin-cancer\\train\\benign')
for i in random.sample(glob.glob('*'), 500):
    shutil.move(i, 'C:\\Users\\רועי\\data\\skin-cancer\\benign_set')  
os.chdir('../../')
os.removedirs('C:\\Users\רועי\\data\\skin-cancer\\train\\benign')
os.chdir('../../')

# Delete data from train directories
os.chdir('C:\\Users\\רועי\\data\\skin-cancer\\train\\malignant')
if os.path.isdir('C:\\Users\\רועי\\data\\skin-cancer\\malignant_set') is False:
    os.makedirs('C:\\Users\רועי\\data\\skin-cancer\\malignant_set')
for i in random.sample(glob.glob('*'), 500):
    shutil.move(i, 'C:\\Users\\רועי\\data\\skin-cancer\\malignant_set')  
os.chdir('../../')
os.removedirs('C:\\Users\רועי\\data\\skin-cancer\\train\\malignant')
os.chdir('../../')
In [56]:
# Delete data from train directories
os.chdir('C:\\Users\\רועי\\data\\skin-cancer\\valid\\benign')
for i in random.sample(glob.glob('*'), 100):
    shutil.move(i, 'C:\\Users\\רועי\\data\\skin-cancer\\benign_set')  
os.chdir('../../')
os.removedirs('C:\\Users\רועי\\data\\skin-cancer\\valid\\benign')
os.chdir('../../')

# Delete data from valid directories
os.chdir('C:\\Users\\רועי\\data\\skin-cancer\\valid\\malignant')
if os.path.isdir('C:\\Users\\רועי\\data\\skin-cancer\\malignant_set') is False:
    os.makedirs('C:\\Users\רועי\\data\\skin-cancer\\malignant_set')
for i in random.sample(glob.glob('*'), 100):
    shutil.move(i, 'C:\\Users\\רועי\\data\\skin-cancer\\malignant_set')  
os.chdir('../../')
os.removedirs('C:\\Users\רועי\\data\\skin-cancer\\valid\\malignant')
os.chdir('../../')
In [57]:
# Delete data from test directories
os.chdir('C:\\Users\\רועי\\data\\skin-cancer\\test\\benign')
for i in random.sample(glob.glob('*'), 50):
    shutil.move(i, 'C:\\Users\\רועי\\data\\skin-cancer\\benign_set')  
os.chdir('../../')
os.removedirs('C:\\Users\רועי\\data\\skin-cancer\\test\\benign')
os.chdir('../../')

# Delete data from test directories
os.chdir('C:\\Users\\רועי\\data\\skin-cancer\\test\\malignant')
if os.path.isdir('C:\\Users\\רועי\\data\\skin-cancer\\malignant_set') is False:
    os.makedirs('C:\\Users\רועי\\data\\skin-cancer\\malignant_set')
for i in random.sample(glob.glob('*'), 50):
    shutil.move(i, 'C:\\Users\\רועי\\data\\skin-cancer\\malignant_set')  
os.chdir('../../')
os.removedirs('C:\\Users\רועי\\data\\skin-cancer\\test\\malignant')
os.chdir('../../')
In [58]:
if randy == 0:
    # Delete data from pred directories
    os.chdir('C:\\Users\\רועי\\data\\skin-cancer\\pred\\benign')
    for i in random.sample(glob.glob('*'), 1):
        shutil.move(i, 'C:\\Users\\רועי\\data\\skin-cancer\\benign_set')  
    os.chdir('../../')
    os.removedirs('C:\\Users\רועי\\data\\skin-cancer\\pred\\benign')
else:
    # Delete data from pred directories
    os.chdir('C:\\Users\\רועי\\data\\skin-cancer\\pred\\malignant')
    for i in random.sample(glob.glob('*'), 1):
        shutil.move(i, 'C:\\Users\\רועי\\data\\skin-cancer\\malignant_set')  
    os.chdir('../../')
    os.removedirs('C:\\Users\רועי\\data\\skin-cancer\\pred\\malignant')